﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BZV.Foundation
{
    /// <summary>
    /// 用户账户
    /// </summary>
    public class UserCount
    {
#region 内部数据
        private double dSixBouns;                // 组六的奖金
        private double dThreeBouns;              // 组三的奖金
#endregion

#region 外部数据
        public int nTotalTimes;                // 总次数
        public int nCorrectTimes;              // 正确次数
        public double dTotalMoney;             // 总钱数
        public double dCorrectPercent;         // 正确率
        public double dMaxCost;                // 最大的钱数
#endregion
        public UserCount()
        {
            InitialData();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitialData()
        {
            nTotalTimes = 0;
            nCorrectTimes = 0;
            dTotalMoney = 0;
            dSixBouns = 0;
            dTotalMoney = 0;
            dCorrectPercent = 0;
            dMaxCost = 0;
        }

        /// <summary>
        /// 获取下注金额
        /// </summary>
        /// <param name="state"></param>
        /// <param name="strBetCode"></param>
        /// <returns></returns>
        public double GetBetMoney(CombinationState state, string strBetCode)
        {
            double[] sixBetMoney = { 2, 8, 20, 40, 70, 112, 168, 240 };
            double[] threeBetMoney = { 4, 12, 24, 40, 60, 84, 112, 144, 180};

            // 获取金额
            int nCount = strBetCode.Length;
            double nMoney = 0;
            if (state == CombinationState.EN_COM_SIX)
            {
                if (nCount < 3)
                {
                    return 0;
                }

                return sixBetMoney[nCount - 3];
            }
            else if (state == CombinationState.EN_COM_THREE)
            {
                if (nCount < 2)
                {
                    return 0;
                }
                return threeBetMoney[nCount - 2];
            }

            return 0;
        }

        /// <summary>
        /// 检查是否正确
        /// </summary>
        /// <param name="state"></param>
        /// <param name="strBetCode"></param>
        /// <param name="strRightCode"></param>
        /// <returns></returns>
        public static bool CheckRight(CombinationState state, string strBetCode, string strRightCode)
        {
            // 判断组态是否正确
            string strLastCode = strRightCode.Substring(2, 3);
            CombinationState curState = BasicParam.GetComState(strLastCode);
            if (curState != state)
            {
                return false;
            }

            // 判断数值是否正确
            if (strBetCode.Contains(strLastCode[0])
                    && strBetCode.Contains(strLastCode[1])
                    && strBetCode.Contains(strLastCode[2]))
            {
                return true;
            }

            return false;
        }
        
        /// <summary>
        /// 计算参数
        /// </summary>
        /// <param name="state"></param>
        /// <param name="bIsRight"></param>
        /// <param name="dDetMoney"></param>
        public  void Calc(CombinationState state, bool bIsRight, double dBetMoney, int nTimes)
        {
            dTotalMoney -= dBetMoney * nTimes;
            if (state == CombinationState.EN_COM_SIX)
            {
                if (bIsRight)
                {
                    dTotalMoney += dSixBouns * nTimes;
                }
            }
            else if (state == CombinationState.EN_COM_THREE)
            {
                if (bIsRight)
                {
                    dTotalMoney += dThreeBouns * nTimes;
                }
            }

            if (dTotalMoney < dMaxCost)
            {
                dMaxCost = dTotalMoney;
            }

            // 次数计算
            if (bIsRight)
            {
                // 增加
                nCorrectTimes++;

                // 计算百分比
                dCorrectPercent = nCorrectTimes / (nTotalTimes * 1.0);
            }
        }
        /// <summary>
        /// 设置奖金
        /// </summary>
        /// <param name="sixBouns"></param>
        /// <param name="threeBouns"></param>
        public void SetBouns(double sixBouns, double threeBouns)
        {
            dSixBouns = sixBouns;
            dThreeBouns = threeBouns;
        }

        /// <summary>
        /// 下注并且给出时候成功
        /// </summary>
        /// <param name="state"></param>
        /// <param name="strBetCode"></param>
        /// <param name="nTimes"></param>
        /// <param name="strCorCode"></param>
        /// <returns></returns>
        public bool SetBet(CombinationState state, string strBetCode, int nTimes, string strCorCode)
        {
            // 下注金额
            double dBetMoney = GetBetMoney(state, strBetCode);
            if (dBetMoney <= 0)
            {
                //throw new Exception("投注错误");
                return false;
            }

            // 判断是否正确
            bool isRight = CheckRight(state, strBetCode, strCorCode);
            nTotalTimes++;

            // 计算金额和相关参数
            Calc(state, isRight, dBetMoney, nTimes);

            return isRight;
        }
    }
}
